// pages/myorder/myorder.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNone: false, // 是否为空
    status: 1, // 1全部 10待支付  20待发货  30待收货  40待评价
    type: 1, // 1商家配送 2自提
    list: [], // 列表
    page: 0,
    pagesize: 10,
    noMore: false, // 没有更多了
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      status: options.type
    })

    if(options.status){
      this.setData({
        type: options.status
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData();
    this.getData();
  },

  // 获取数据
  getData(){
    app.loading();
    let that = this;
    wx.request({
      url: app.baseURL + 'order/getOrderLists',
      data: {
        order_status: that.data.status,
        order_type: 10,
        is_status: that.data.type,
        token: app.globalData.userId,
        page: ++that.data.page,
        pagesize: that.data.pagesize
      },
      success(data){
        let res = data.data;
        wx.hideLoading();
        if(res.code == 1){
          if(res.data.list.length > 0){
            that.setData({
              list: [...that.data.list,...res.data.list],
              noMore: Number(res.data.list.length) >= Number(that.data.pagesize) ? false :true
            })
          }

          if(that.data.list.length > 0){
            that.setData({
              isNone: false,
            })
          }else{
            that.setData({
              isNone: true,
              list: [],
            })
          }
        }else{
          app.$tip(res.msg);
          that.setData({
            isNone: true
          })
        }
      }
    })
  },

  // 格式化
  initData(){
    this.setData({
      list: [], // 列表
      page: 0,
    })
  },

  // 改变订单类型 1配送 2自提
  changeType(e){
    if(this.data.status == 20 || this.data.status == 30){
      this.setData({
        status: 1
      })
    }
    this.setData({
      type: e.currentTarget.dataset.type,
      list: [],
      page: 0
    })
    this.getData();
  },

  // 改变订单状态 1全部 10待支付  20待发货  30待收货  40待评价
  changeStatus(e){
    this.setData({
      status: e.currentTarget.dataset.status,
      list: [],
      page: 0,
    })
    this.getData();
  },

  // 查看详情
  toDetail(e){
    let orderid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/order_detail/order_detail?id='+ orderid,
    })
  },

  // 查看物流
  toLook(e){
    let orderid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/logistics/logistics?order_id=' + orderid,
    })
  },

  // 立即提货
  toRecive(e){
    wx.navigateTo({
      url: '/pages/receiveCode/receiveCode?id=' + e.currentTarget.dataset.id,
    })
  },

  // 订单支付
  toPay(e){
    let order_no = e.currentTarget.dataset.order_no, that = this;
    app.loading();
    wx.request({
      url: app.baseURL + 'pay/pay',
      data: {
        token: app.globalData.userId,
        order_no: order_no
      },
      success(data){
        let res = data.data;
        if(res.code == 1){
          that.wxPay(res.data);
        }else{
          wx.hideLoading();
          app.$tip(res.msg);
        }
      }
    })
  },

  // 微信支付
  wxPay(data){
    let that = this;
    wx.requestPayment({
      timeStamp: data.timeStamp,
      nonceStr: data.nonceStr,
      package: data.package,
      signType: data.signType,
      paySign: data.paySign,
      success(res){
        wx.hideLoading();
        app.$tip('支付成功!');
        setTimeout(() => {
          that.getData();
          that.initData();
        },1500)
      },
      fail(err){
        wx.hideLoading();
        if (err.errMsg == 'requestPayment:fail cancel'){
          app.$tip('支付取消!');
        }else{
          app.$tip('支付失败!');
        }
      }
    })
  },

  // 取消订单
  cancelOrder(e){
    let orderid = e.currentTarget.dataset.id,that = this;
    wx.showModal({
      title: '温馨提示',
      content: '您确定要取消当前订单吗?',
      confirmColor: '#5CA032',
      success(res){
        if(res.confirm){
          app.loading();
          wx.request({
            url: app.baseURL + 'order/setCancelOrder',
            data: {
              order_id: orderid,
              token: app.globalData.userId
            },
            success(data){
              let ress = data.data;
              wx.hideLoading();
              if(ress.code == 1){
                app.$tip(ress.msg)
                setTimeout(() =>{
                  that.initData();
                  that.getData();
                },1500)
              }else{
                app.$tip(ress.msg)
              }
            }
          })
        }
      }
    })
  },

  // 删除订单
  delOrder(e){
    let orderid = e.currentTarget.dataset.id,that = this;
    wx.showModal({
      title: '温馨提示',
      content: '您确定要删除当前订单吗?',
      confirmColor: '#5CA032',
      success(res){
        if(res.confirm){
          app.loading();
          wx.request({
            url: app.baseURL + 'order/setDelOrder',
            data: {
              order_id: orderid,
              token: app.globalData.userId
            },
            success(data){
              let ress = data.data;
              wx.hideLoading();
              if(ress.code == 1){
                app.$tip(ress.msg)
                setTimeout(() =>{
                  that.initData();
                  that.getData();
                },1500)
              }else{
                app.$tip(ress.msg)
              }
            }
          })
        }
      }
    })
  },

  // 确认收货
  recived(e){
    let orderid = e.currentTarget.dataset.id,that = this;
    wx.showModal({
      title: '温馨提示',
      content: '您确定要确认收货吗?',
      confirmColor: '#5CA032',
      success(res){
        if(res.confirm){
          app.loading();
          wx.request({
            url: app.baseURL + 'order/confirm_receipt',
            data: {
              order_id: orderid,
              token: app.globalData.userId
            },
            success(data){
              let ress = data.data;
              wx.hideLoading();
              if(ress.code == 1){
                app.$tip(ress.msg)
                setTimeout(() =>{
                  that.initData();
                  that.getData();
                },1500)
              }else{
                app.$tip(ress.msg)
              }
            }
          })
        }
      }
    })
  },

  // 去评价
  toEvaluate(e){
    wx.navigateTo({
      url: '/pages/pub_comment/pub_comment?id=' + e.currentTarget.dataset.id,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.noMore){
      // this.setData({
      //   page: this.data.page + 1
      // })
      this.getData();
    }
  },

})